from pathlib import Path
from PIL import Image
import time

d1 = r'M:\图\网图'
pic_ext = ['jpg', 'png', 'gif', 'bmp', 'jpeg', 'webp']
Image.MAX_IMAGE_PIXELS = 1000000000

count = 0
# 获取当前时间戳
start_time = time.time()


def move(p_path, dest):
    try:
        dest.mkdir(exist_ok=True)
        p_path.replace(dest / p_path.name)
        print(p_path.name, '已移动到', dest, '目录下')
    except Exception as e:
        print(e)


pic_list = []
for path in Path(d1).glob('*.*'):
    if path.suffix.lower()[1:] in pic_ext:
        pic_list.append(path)
total = len(pic_list)
print('共找到', total, '张图片')

for p in pic_list:
    count += 1
    print(f'{count}/{total} ({round((count / total) * 100)}%)')
    with Image.open(p) as im:
        w, h = im.size

    long = max(w, h)
    if h is None:
        no_info_dir = p.parent / 'no_info'
        move(p, no_info_dir)
    elif long < 700:
        small_dir = p.parent / 'small'
        move(p, small_dir)
    elif w / h > 2.5:
        heng_dir = p.parent / 'heng'
        move(p, heng_dir)
    elif h / w > 2.5:
        long_dir = p.parent / 'long'
        move(p, long_dir)

end_time = time.time()
print('共处理', count, '张图片，耗时', end_time - start_time, '秒')
